// pages/qiandao/qiandao.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    my: [],
    his: [],
    kkkkkkk: 's1',
    is:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    this.getX()
    var that = this
    var query = wx.createSelectorQuery();
    query.select('#zong').boundingClientRect(function (rect) {
      console.log(rect)
      that.setData({
        kkkkkskk: rect.height * 0.8,

      })
      console.log("sss", that.data.kkkkkkk)
    }).exec();
  },
  onShow: function (e) {
    var that = this
    var myDate = new Date();

    var day = myDate.getDate(); //获取当前日(1-31)
    setTimeout(function (e) {
      that.setData({
        kkkkkkk: 's' + (day - 2),
      })
    }, 1000)

  },
  getX() {
    var that = this
    let tableName = 'qiandao'
    let Query = new wx.BaaS.Query()
    let Query1 = new wx.BaaS.Query()
    Query.compare('phone', '=', wx.getStorageSync('phone'))
    Query1.compare('phone', '=', "1")
    let orQuery = wx.BaaS.Query.or(Query, Query1)
    let Product = new wx.BaaS.TableObject(tableName)
    Product.setQuery(orQuery).limit(1000).offset(0).find().then(res => {
      // success
      console.log(res)
      for (var i = 0; i < res.data.objects.length; i++) {
        if (res.data.objects[i].phone == "1") {
          that.setData({
            his: res.data.objects[i]
          })
        } else {
          that.setData({
            my: res.data.objects[i]
          })
        }
      }
      var kkk = 1
      for(var k = 1  ; k<that.data.my.date.length ; k++){
        var q = that.data.my.date[k].split('-')
        var h = that.data.my.date[k-1].split('-')
        console.log(parseInt(h[2])+1)
        if(q[0] == h[0] && q[1] == h[1] && q[2] == (parseInt(h[2])+1)){
          kkk++
        }
      }
      that.setData({
        kkss:kkk
      })
      if(this.data.is == false ){
        this.getY()
      }
     
    }), err => {
      // err
    }
  },
  getY() {
    var that = this
    var myDate = new Date();
    var year = myDate.getFullYear(); //获取完整的年份(4位,1970-????)
    var mouth = myDate.getMonth() + 1; //获取当前月份(0-11,0代表1月)
    var day = myDate.getDate(); //获取当前日(1-31)
    var d = myDate.getDay(); //获取当前星期X(0-6,0代表星期天)
    var hour = myDate.getHours()
    var min = myDate.getMinutes()
    var sce = myDate.getSeconds()
    var days = new Date(year, mouth, 0).getDate();
    // console.log(mouth)
    // console.log(days)
    var that = this

    that.setData({
      kkkkkkk: 's' + (day - 1),
    })
    var items = []

    for (var i = 1; i <= days; i++) {
      var s = 0
      // console.log(that.data.his)
      if (i == day) {
        var item = {
          date: year + '-' + mouth + '-' + i,
          time: hour + ':' + min + ':' + sce,
          content: '',
          pm: '',
          am: '',
          day: d,
          istoday: 1,
          id: i,
          isR: 0
        }

      } else {
        var item = {
          date: year + '-' + mouth + '-' + i,
          time: hour + ':' + min + ':' + sce,
          content: '',
          pm: '',
          am: '',
          day: d,
          istoday: 0,
          id: i,
          isR: 0
        }
      }
      for (var j = 0; j < that.data.his.date.length; j++) {
        var kk = year + '-' + mouth + '-' + i
        // console.log(kk)
        var k = that.data.his.date[j].split(' ')
        // console.log(k)
        var contents = that.data.his.content.split(";")
        // console.log(contents)
        if (k[0].split('-')[0] == year && k[0].split('-')[1] == mouth && k[0].split('-')[2] == i) {
          // console.log(555555555555)
          item.content = contents[s]
          if (k[1].split(':')[0] < 12) {
            item.am = k[1]
          } else {
            item.pm = k[1]
          }
        }
      }
      for (var j = 0; j < that.data.my.date.length; j++) {

        var k = that.data.my.date[j]
        // console.log(contents)
        if (k.split('-')[0] == year && k.split('-')[1] == mouth && k.split('-')[2] == i) {
          // console.log(555555555555)
          item.isR = 1
        }
      }

      items.push(item)
    }
    // console.log(items)
    that.setData({
      items: items
    })
  },
  qiandao: function (e) {
    console.log(e.currentTarget.dataset.id)

    var that = this
    var myDate = new Date();
    var year = myDate.getFullYear(); //获取完整的年份(4位,1970-????)
    var mouth = myDate.getMonth() + 1; //获取当前月份(0-11,0代表1月)
    var day = myDate.getDate(); //获取当前日(1-31)
    var dae = year + '-' + mouth + '-' + day
    // 元素类型为 string
    var tableName = "qiandao"
    let Product = new wx.BaaS.TableObject(tableName)
    let product = Product.getWithoutData(that.data.my.id)
    product.append('date', dae)
    product.update().then(res=>{
      // success
      wx.showToast({
        title: '签到成功',
        icon:'success',
        duration:2000
      })
      that.data.items[e.currentTarget.dataset.id].isR = 1
      that.setData({
        items : that.data.items,
        is:true
      })
      that.getX()
    })

    
  }
})